package cn.itsource.org.controller;

import cn.itsource.basic.query.BaseQuery;
import cn.itsource.basic.query.PageList;
import cn.itsource.basic.utils.AjaxResult;
import cn.itsource.org.domain.Department;
import cn.itsource.org.query.DepartmentQuery;
import cn.itsource.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/dept")
@Api(value = "DepartmentController",tags = {"部门接口文档"})
public class DepartmentController {

    //注入service层
    @Autowired
    private IDepartmentService departmentService;


    /**
     * 分页查询
     * @param query
     * @return
     */
    @PostMapping("/list")
    @ApiOperation(value = "getPageList",notes = "返回所有的分页数据")
    public AjaxResult getPageList(@ApiParam(value = "分页参数",name = "分页查询")@RequestBody DepartmentQuery query){
        return AjaxResult.creatSuccess(departmentService.getAllByPage(query));
    }

    /**
     * 新增或者修改操作
     * @param department
     * @return
     */
    @PostMapping("/saveOrUpdate")
    public AjaxResult saveOrUpdate(@RequestBody Department department){
        try {
            departmentService.saveOrUpdate(department);
            return AjaxResult.creatSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.creatError("操作失败，找土皮回炉");
        }
    }

    //删除
    @DeleteMapping("/delete/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){

        try {
            departmentService.delete(id);
            return AjaxResult.creatSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.creatError("删除操作失败，找土皮回炉");
        }
    }

    /**
     * 部门的无限级数据
     * @return
     */
    @GetMapping("/treeList")
    public AjaxResult treeList(){
        return AjaxResult.creatSuccess(departmentService.treeDepts());
    }

    /**
     * 父级部门数据
     * @return
     */
    @GetMapping("/getParentDepts")
    public AjaxResult getParentDepts(){
        return AjaxResult.creatSuccess(departmentService.getParentDepts());
    }
}
